﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;


namespace SCCL2010
{
    public static class ContentTypes
    {
        /// <summary>
        /// Checks if the ContentType exists in the SPList.
        /// </summary>
        /// <param name="oList"></param>
        /// <param name="cContentTypeName"></param>
        /// <returns>returns a boolean value if the ContentType exists in the Supplied List.</returns>
        public static bool HasContentType(SPList oList, string cContentTypeName)
        {
            foreach (SPContentType ct in oList.ContentTypes)
            {
                if (ct.Name == cContentTypeName)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Checks if the ContentType exists in the SPWeb.
        /// </summary>
        /// <param name="oWeb"></param>
        /// <param name="cContentTypeName"></param>
        /// <returns>returns a boolean value if the ContentType exists in the supplied SPWeb.</returns>
        public static bool ContentTypeExists(SPWeb oWeb, string cContentTypeName)
        {
            foreach (SPContentType ct in oWeb.ContentTypes)
            {
                if (ct.Name == cContentTypeName)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Retrieves the ContentType object based on the supplied ContentTypeName.
        /// </summary>
        /// <param name="oWeb"></param>
        /// <param name="cContentTypeName"></param>
        /// <returns>returns a SPContentType object </returns>
        public static SPContentType GetContentType(SPWeb oWeb, string cContentTypeName)
        {
            foreach (SPContentType ct in oWeb.ContentTypes)
            {
                if (ct.Name == cContentTypeName)
                {
                    return ct;
                }
            }
            return null;
        }

        /// <summary>
        /// Retrieves ContentTypeID based on the ContentTypeName.
        /// </summary>
        /// <param name="oWeb"></param>
        /// <param name="cContentTypeName"></param>
        /// <returns>returns a string value representing the ContentTypeID for the ContentTypeName.</returns>
        public static string GetContentTypeID(SPWeb oWeb, string cContentTypeName)
        {
            foreach (SPContentType ct in oWeb.ContentTypes)
            {
                if (ct.Name == cContentTypeName)
                {
                    return ct.Id.ToString();
                }
            }
            return null;
        }

        /// <summary>
        /// Method to add the ContentType to the ContentTypes collection.
        /// </summary>
        /// <param name="oWeb"></param>
        /// <param name="cContentTypeName"></param>
        /// <param name="Description"></param>
        /// <param name="oParent"></param>
        public static void AddContentType(SPWeb oWeb, string cContentTypeName, string Description, SPContentType oParent)
        {
            SPContentType oNewContentType = new SPContentType(oParent, oWeb.ContentTypes, cContentTypeName) { Description = Description };
            oNewContentType.Update();
            oWeb.ContentTypes.Add(oNewContentType);
        }

    }
}
